package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.CartListByIdRequest;
import com.mall.shopping.dto.CartListByIdResponse;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/shopping")
public class CartsController {

    @Reference
    ICartService iCartService;

    @GetMapping("/carts")
    public ResponseData getCartsInfo(HttpServletRequest request) {
//        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo.toString());
//        Long uid = Long.parseLong(object.get("uid").toString());
//
//        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
//        cartListByIdRequest.setUserId(uid);
        Long userInfo = (Long) request.getAttribute(TokenIntercepter.USER_INFO_KEY);

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userInfo);
        CartListByIdResponse cartListByIdResponse = iCartService.getCartListById(cartListByIdRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(cartListByIdResponse.getCode())) {
            return new ResponseUtil<>().setData(cartListByIdResponse.getCartProductDtos());
        }

        return new ResponseUtil<>().setErrorMsg(cartListByIdResponse.getMsg());
    }
}
